//
//  FancyUser.swift
//  Fancyswap
//
//  Created by Francois Gratton on 2015-03-30.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import Foundation

class FancyUser {
    
    var city: String = ""
    var email: String = ""
    var locale: String = ""
    var username: String = ""
    var zip: String = ""
    var firstname: String = ""
    var lastname: String = ""
    var address: String = ""
    var province: String = ""
    var country: String = ""
    var balance: Int = 0
    var shipping: Int = 0
    var token: String = ""
    
    init(){
    }
    
    /// create using json
    init (dic:NSDictionary) {
        let sjson = JSON(dic)
        importData(sjson)
    }
    
    init (swiftyDic:JSON) {
        importData(swiftyDic)
    }

    func importData (swiftyDic:JSON) {
        for (key, value) in swiftyDic
        {
            switch (key).lowercaseString
            {
            case "city":
                self.city = value.stringValue
            case "email":
                self.email = value.stringValue
            case "locale":
                self.locale = value.stringValue
            case "username":
                self.username = value.stringValue
            case "account":
                self.balance = value.intValue
            case "zip":
                self.zip = value.stringValue
            case "token":
                self.token = value.stringValue
            case "shippingfee":
                self.shipping = value.intValue
            case "address":
                for (key2, value2) in value
                {
                    switch (key2).lowercaseString
                    {
                    case "city":
                        self.city = value2.stringValue
                    case "address":
                        self.address = value2.stringValue
                    case "country":
                        self.country = value2.stringValue
                    case "zip":
                        self.zip = value2.stringValue
                    case "lastname":
                        self.lastname = value2.stringValue
                    case "firstname":
                        self.firstname = value2.stringValue
                    case "state":
                        self.province = value2.stringValue
                    default:
                        println("Unknown address element: \(key2):\(value2)")
                    }
                }
                
            default:
                println("Unknown user element: \(key):\(value)")
            }
        }
    }
    
    func toString() -> String {
        return "username: \(username), email: \(email), locale: \(locale), city: \(city), zip: \(zip), firstname: \(firstname), lastname: \(lastname), address: \(address), province: \(province), country: \(country), balance: \(balance), shipping: \(shipping)"
    }
}